package DTI;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;

public class WordAnalyzer {

	private static Vector stop_words_;
	private File stop_file_;
	
	  /**
	   * Constructs a word analyzer based on the specified File stop_file.
	   * 
	   * @param stop_file The file that contains the stop words to be used.
	   * @throws IOException if the File in_stopFile can not be read.
	   * @throws FileNotFoundException if the File stop_file does not exist
	   */
	public WordAnalyzer(File stop_file) throws IOException{
		stop_file_ = stop_file;
		stop_words_ = new Vector();
		computeStopWords(stop_file);
	}
	
	  /**
	   * Method that returns true if stop_words_ contains word.  Returns false if it does not.
	   * 
	   * @param word String object that may or may not be contained in the stop_words_ List
	   */
	
	public boolean isStopWord(String word){
		if(stop_words_.contains(word))
			return true;			
		else
			return false;
	}
	
	public String getStopFileLocation(){
		return stop_file_.getAbsolutePath();
	}
	
	  /**
	   * Constructs the List of "stop words" from the input File stop_file, determining
	   * 			stop words based on the the the specified set of breaker characters
	   * 
	   * @param stop_file the File object that contains all the "stop" words
	   * 			which will be used to comprise the WordAnalyzer object
	   * @throws IOException if the File stop_file experiences an problem
	   * 			while being read.  The most common exception will be if
	   * 			the stop File does not have read permission
	   * @throws FileNotFoundException if the File stop_file does not exist
	   */
	
	private static void computeStopWords(File stop_file) throws IOException{
		if(!stop_file.equals(null)){
			if (!stop_file.exists()) {
		      throw new FileNotFoundException("Error: file" + stop_file + " not found. Please check the file name or path and try again.");
		    }
		    try {
		      FileInputStream fileInputStream = new FileInputStream(stop_file);
		     
		      char next = (char)fileInputStream.read();
		      String current_word = "";
		      boolean in_word = false;
		      
			  while(next != 65535){
		    	  if(CharacterAnalyzer.isBreakCharacter(next)){
		    		  if(in_word){
		    			  in_word = false;
		    			  current_word = current_word.toLowerCase();
		    			  if(!stop_words_.contains(current_word)){
		    				  stop_words_.add(current_word);
		    			  }
		    			  current_word = "";
		    		  }
		    	  }
		    	  else{
		    		 if(in_word == false)
		    			 in_word = true;
		    		 current_word = current_word.concat(new Character(next).toString());
		    	  }
		    	  next = (char)fileInputStream.read();
		      }
		      
		      if(next == 65535 || next == -1){
		    	  current_word = current_word.toLowerCase();
    			  stop_words_.add(current_word);
		      }
		      
		      fileInputStream.close();
		      
		    } catch(IOException e) {
		      throw new IOException("Error: cannot read file" + stop_file + ". Please check your permissions on this file and try again.");
		    }
		}
	}
}
